const { thingListModel } = require('../modules/thingListModule');
//添加物料
async function addThing(req, res, next) {
    const data = req.query;
    const result = await thingListModel.create(data);
    if (result) {
        res.send({
            message: "添加成功",
            code: 200
        })
    } else {
        res.send({
            message: "添加失败",
            code: 500
        })
    }
}
//查找物料
async function getThing(req, res, next) {
    const { value, currentPage, pageSize } = req.query;
    //获取数据库总数据
    const total = await thingListModel.countDocuments({
        $or: [
            { thingName: { $regex: value, $options: '$i' } },
            { thingType: { $regex: value, $options: '$i' } },
            { thingBrand: { $regex: value, $options: '$i' } },
            { thingUnit: { $regex: value, $options: '$i' } },
            { thingNote: { $regex: value, $options: '$i' } },
        ]
    });
    //换算成对应的页数
    const pages = Math.ceil(total / pageSize);

    const result = await thingListModel
        .find({
            $or: [
                { thingName: { $regex: value, $options: '$i' } },
                { thingType: { $regex: value, $options: '$i' } },
                { thingBrand: { $regex: value, $options: '$i' } },
                { thingUnit: { $regex: value, $options: '$i' } },
                { thingNote: { $regex: value, $options: '$i' } },
            ]
        })
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
    if (result.length > 0) {
        res.send({
            message: "获取成功",
            code: 200,
            result,
            total,
            pages
        })
    } else {
        res.send({
            message: "获取失败",
            code: 500,
        })
    }
}
//删除物料
async function delThing(req, res, next) {
    const data = req.body;
    const result = await thingListModel.deleteOne(data);
    if (result.deletedCount > 0) {
        res.send({
            message: "删除成功",
            code: 200
        })
    } else {
        res.send({
            message: "删除失败",
            code: 200
        })
    }
}
//查找物料
async function findThing(req, res, next) {
    const data = req.body;
    const result = await thingListModel.find(data);
    if (result.length > 0) {
        res.send({
            message: '获取成功',
            code: 200,
            result
        })
    } else {
        res.send({
            message: "获取失败",
            code: 500
        })
    }
}
//查找物料
async function changeThing(req, res, next) {
    const data = req.body;
    const result = await thingListModel.updateOne({ _id: data._id }, data);
    if (result.nModified > 0) {
        res.send({
            message: "修改成功",
            code: 200
        })
    } else {
        res.send({
            message: "修改失败",
            code: 500
        })
    }
}
//多ID查询
async function findMore(req, res, next) {
    const { arr } = req.body
    const result = await thingListModel.find({ _id: { $in: arr } });
    if(result.length>0){
        res.send({
            message:"获取成功",
            code:200,
            result
        })
    }else{
        res.send({
            message:'获取失败',
            code:500
        })
    }
}


module.exports = {
    addThing, getThing, delThing, findThing, changeThing, findMore
}